package com.qifenqian.turbot.module.ui

import android.Manifest
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.Constant
import com.qifenqian.turbot.config.TurbotEnum
import com.qifenqian.turbot.module.contract.AccountContract
import com.qifenqian.turbot.module.entity.response.LoginEntity
import com.qifenqian.turbot.module.entity.response.VersionEntity
import com.qifenqian.turbot.module.presenter.AccountPresenter
import com.qifenqian.turbot.module.ui.loginetc.LoginActivity
import com.qifenqian.turbot.util.SpUtils
import com.tbruyelle.rxpermissions2.RxPermissions
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import org.jetbrains.anko.toast
import java.util.concurrent.TimeUnit

/**
 * @author H
 * @createDate 2018/12/13 11:25
 * @description SplashActivity
 * @see
 * @since
 */
class SplashActivity : BaseActivity<AccountPresenter, AccountContract.View>(), AccountContract.View {

    override fun bindLayoutId(): Int {
        return R.layout.activity_splash
    }

    override fun bindPresenter(): AccountPresenter? {
        return AccountPresenter(this, this)
    }

    var isLogin = SpUtils.getInstance().getString(Constant.SP_KEY_PASSWORD, Constant.SP_ACCOUNT).isNotEmpty()

    override fun initial() {
        // 2018/12/25 请求所需要的所有权限 ----->
        RxPermissions(this)
                .request(Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA)
                .subscribe {
                    mPresenter!!.checkVersion()
                }
    }

    private fun countTimer(versionEntity: VersionEntity?) {
        Observable.intervalRange(0, 3, 0, 500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<Long> {
                    override fun onNext(t: Long) {
                    }

                    var mDisposable: Disposable? = null

                    override fun onSubscribe(d: Disposable) {
                        mDisposable = d
                    }

                    override fun onError(e: Throwable) {
                    }

                    override fun onComplete() {
                        if (isLogin) { // versionEntity != null 说明网络正常,可以正常登录, 不然在网络错误的时候会报mView错误,需要 fixme
                            mPresenter!!.login(SpUtils.getInstance().getString(Constant.SP_KEY_USER_CODE, Constant.SP_ACCOUNT), SpUtils.getInstance().getString(Constant.SP_KEY_PASSWORD, Constant.SP_ACCOUNT), TurbotEnum.LOGIN_TYPE_PASSWORD.type, showDialog = false)
                        } else {
                            LoginActivity.startThis(this@SplashActivity, versionEntity)
                            finish()
                        }
                        if (mDisposable != null && !mDisposable!!.isDisposed) {
                            mDisposable!!.dispose()
                        }
                    }
                })
    }

    override fun checkVersionResult(isSuccess: Boolean, code: Int, msg: String?, versionEntity: VersionEntity?) {
        if (isSuccess) {
            countTimer(versionEntity!!)
        } else {
            countTimer(null)
            toast(msg!!)
        }
    }

    override fun loginResult(isSuccess: Boolean, code: Int, msg: String?, loginEntity: LoginEntity?, versionEntity: VersionEntity?, password: String?) {
        if (isSuccess) {
            MainActivity.startThis(this@SplashActivity, versionEntity)
            SpUtils.getInstance().putInt(Constant.SP_KEY_RESTAURANT_ID, loginEntity!!.restaurantId, Constant.SP_ACCOUNT)
        } else {
            LoginActivity.startThis(this@SplashActivity)
        }
        finish()
    }
}
